package com.beone.smart.smartBeauty.config;

import org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger2文档配置
 *
 * @author lc
 * 2018-11-16 18:37
 */
@Component
@EnableSwagger2
@Profile({"dev", "test"})
//@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class Swagger2Config {
    @Bean
    public Docket createRestApi() {
        ParameterBuilder params = new ParameterBuilder();
        List<Parameter> list = new ArrayList<>();
        // 在swagger文档中构建token参数
        params.name("x-user-token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        list.add(params.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
//                .host("主机名：端口：服务前缀")  //注意这里的主机名：端口是网关的地址和端口
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.beone.smart.smartBeauty.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(list);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(" SMART BEAUTY")
                .description("智慧美容")
                .version("1.0.0")
                .build();
    }
}
